const NewsModel = require("../../models/NewsModle")

const NewsService = {
  add: async ({ title, content, category, isPublish, cover, editTime, userid }) => {
    return NewsModel.create({
      title, content, category, isPublish, cover, editTime, userid
    })
  },
  getList: async ({ _id }) => {
    return _id ? NewsModel.find({ _id }) : NewsModel.find({})
  },
  delList: async ({ _id }) => {
    return NewsModel.deleteOne({ _id })
  },
  publish: async ({ _id, isPublish, editTime }) => {
    return NewsModel.updateOne({
      _id
    }, {
      isPublish, editTime
    })
  },
  updateList: async ({ title, content, category, isPublish, cover, editTime, _id }) => {
    if (cover) {
      return NewsModel.updateOne({ _id }, {
        title, content, category, isPublish, editTime, cover
      })
    } else {
      return NewsModel.updateOne({ _id }, {
        title, content, category, isPublish, editTime
      })
    }
  }
}
module.exports = NewsService